﻿using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;

namespace Mekiasm
{
    public class CESUBatBox : MachineRotateAbleItem
    {
        public static int Index;

        public CESUBatBox() : base("CESU储电箱")
        {

            textureSlots = new int[] { 137, 138, 139, 138, 140, 140 };
        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }

        public override bool IsInteractive(SubsystemTerrain subsystemTerrain, int value)
        {
            return true;
        }

        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity("储电箱", x, y, z, new List<ComponentEnergyMachine.Slot>(), ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric)
            }), new MachineAmountLimitConfig() { MaxPower = 300 * 1024, OutputPowerLimit = 128, InputPowerLimit = 128 });
            ElectricSystem.Add(new BatBoxElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new BatBoxElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentBatBox mcomponent = blockBehavior.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentBatBox>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new BatBoxWidget(mcomponent, componentMiner);
            return true;
        }

    }
}
